using System;
using System.Collections;
using System.Data;
using System.Data.Common;
using System.Drawing;
using System.Drawing.Printing;
using System.Reflection;
using System.Windows.Forms;
using Syncfusion.Windows.Forms.Grid;
using ctsu.Admin.Forms;
using ctsu.BaseForms;
using ctsu.Security;

namespace ctsu.Admin.BusLogic.CoreLab
{

	public class BLCoreLabFormBaseImpl : BLFormBaseImpl
	{
		#region printing members
		//protected BarTender.Application _btApp;
		protected PrintDialog _pDlg;
		protected PrintDocument _pDoc;
		protected int _page;
		protected int _startRange;
		protected int _startRow;
		protected string _sBatch;
		protected Image _labelImage;
		protected int _curPatient;
		#endregion printing members

		#region control refs
		protected CheckBox _chkPrintReport;
		protected CheckBox _chkPerPatient;
		protected CheckBox _chkPrintSelected;
		#endregion control refs

		public BLCoreLabFormBaseImpl(FormMain formMain, FormBase form, FormInfoBase formInfo, DataTable tableSchema, Permission formSecurity, Hashtable configData)
			: base(formMain, form, formInfo, tableSchema, formSecurity, configData)
		{

		}

		#region initialize
		protected override bool InitializeGrid(FormGridMeta fgm)
		{
			return InitializeCoreLabGrid(fgm);
		}
		protected virtual bool InitializeCoreLabGrid(FormGridMeta fgm)
		{
			GridControl grd = (GridControl)Forms.Utils.GetControlByName(fgm.GridName, this._form.Controls);
			//capture double click here too
			grd.CellDoubleClick += new GridCellClickEventHandler(grd_CellDoubleClick);

			//FormGridMeta formGridMeta, UserGrid uGrid, FormInfoBase formInfo, FormBase form
			Type[] argTypes = new Type[4];
			argTypes[0] = typeof(FormGridMeta);
			argTypes[1] = typeof(GridControl);
			argTypes[2] = typeof(FormInfoBase);
			argTypes[3] = typeof(FormBase);

			object[] args = new object[4];
			args[0] = fgm;
			args[1] = grd;
			args[2] = _formInfo;
			args[3] = _form; //security permission

			//invoke creation of bl
			Type grdBL = Type.GetType("ctsu.Admin.BusLogic." + fgm.BusinessLogicClassName);

			ConstructorInfo info = grdBL.GetConstructor(argTypes);
			IBLCoreLabFormGridBase fgbl = (IBLCoreLabFormGridBase)info.Invoke(args);
			fgbl.GridColumnValueChanged += new ctsu.BaseForms.BusinessLogicEvents.BLGridColumnValueChangedHandler(fgbl_GridColumnValueChanged);
			fgbl.GridValidateError += new ctsu.BaseForms.BusinessLogicEvents.BLGridValidateErrorEventHandler(fgbl_GridValidateError);

			if (!fgbl.Initialize())
				return false;
			_gridBLs.Add(fgm.GridName, fgbl);
			return true;
		}

		protected virtual void grd_CellDoubleClick(object sender, GridCellClickEventArgs e)
		{
			Console.WriteLine("grd_CellDoubleClick");
		}

		protected override void SetGridsEnabled(bool enable)
		{
			SetCoreLabGridsEnabled(enable);
		}
		protected virtual void SetCoreLabGridsEnabled(bool enable)
		{
			foreach (BLCoreLabFormGridBaseImpl fgbl in _gridBLs.Values)
			{
				fgbl.SetEnabled(enable);
			}
		}
		#endregion initialize

		#region get data
		protected override bool GetGridData(int id)
		{
			if (this._gridBLs != null)
			{
				foreach (BLCoreLabFormGridBaseImpl fgbl in this._gridBLs.Values)
				{
					if (!fgbl.GetData(id))
					{
						//TODO - need to do more here for exception							
						MessageBox.Show("GetGridData Exception");
						return false;
					}
				}
			}
			return true;
		}
		#endregion getdata

		#region new
		protected override void SetGridsNew()
		{
			//notify the grids there is a new form record
			if (this._gridBLs != null)
			{
				foreach (BLCoreLabFormGridBaseImpl bl in this._gridBLs.Values)
					bl.New();
			}
		}
		#endregion new

		#region save
		protected override bool VerifyGrids()
		{
			if (this._gridBLs != null)
			{
				foreach (BLCoreLabFormGridBaseImpl fgbl in this._gridBLs.Values)
					if (!fgbl.VerifyGrid())
						return false;
			}
			return true;
		}

		protected override void SetGridsAcceptChanges()
		{
			foreach (BLCoreLabFormGridBaseImpl bl in this._gridBLs.Values)
			{
				bl.AcceptChanges();
			}
		}

		protected override bool SaveGrids(out string message, Microsoft.Practices.EnterpriseLibrary.Data.Database db, DbTransaction trans)
		{
			message = "";
			foreach (BLCoreLabFormGridBaseImpl bl in this._gridBLs.Values)
			{
				if (!bl.Save(this._currentPrimaryID, out message, db, trans))
					return false;
				//resets data to show new id's
				//bl.GetData(this._currentPrimaryID);				
			}
			return true;
		}
		#endregion save

		#region cancel
		protected override void ResetGrids()
		{
			ResetCoreLabGrids();
		}
		protected virtual void ResetCoreLabGrids()
		{
			foreach (BLCoreLabFormGridBaseImpl fgbl in _gridBLs.Values)
				fgbl.Reset(this._formMode, _currentPrimaryID);
		}
		#endregion cancel

		protected bool HasRows()
		{
			Syncfusion.Windows.Forms.Grid.GridControl grid = null;
			foreach (BLCoreLabFormGridBaseImpl fgbl in this._gridBLs.Values)
			{
				grid = fgbl.Grid;
				if (grid.RowCount > 0)
					return true;
			}
			return false;
		}

		protected bool HasRowNoID()
		{
			Syncfusion.Windows.Forms.Grid.GridControl grid = null;
			foreach (BLCoreLabFormGridBaseImpl fgbl in this._gridBLs.Values)
			{
				grid = fgbl.Grid;
				for (int i = 1; i <= grid.RowCount; i++)
				{
					if (grid[i, 1].Text.Length == 0)
						return true;

					if ((int)grid[i, 1].CellValue < 1)
						return true;
				}
			}
			return false;
		}

		protected object ParseFromToolTip(string field, string tip)
		{
			string[] items = tip.Split('\n');
			string item = "";
			string key = "";
			string val = "";
			object obj = null;

			int pos = 0;
			foreach (string s in items)
			{
				if (s.Length == 0)
					continue;
				item = s.Trim();
				pos = item.IndexOf(":");
				key = item.Substring(0, pos);
				val = item.Substring(pos + 1);
				if (key == field)
					return val.Trim();
			}

			return obj;
		}

	}
}
